package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.ClientDevPlan;
import cn.wolfcode.crm.query.ClientPlanQueryObject;
import cn.wolfcode.crm.service.IClientDevPlanService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("clientdevplan")
public class ClientDevPlanController {

    @Autowired
    IClientDevPlanService clientdevplanService;

    @RequestMapping("view")
    @RequiresPermissions(value = {"clientdevplan:view","开发计划页面"},logical = Logical.OR)
    public String view() {
        return "clientdevplan";
    }

    @RequestMapping("query")
    @ResponseBody
    @RequiresPermissions(value = {"clientdevplan:query","开发计划列表"},logical = Logical.OR)
    public PageResult query(ClientPlanQueryObject qo) {
        return clientdevplanService.query(qo);
    }

    @RequestMapping("selectAll")
    @ResponseBody
    public List<ClientDevPlan> selectAll() {
        return clientdevplanService.selectAll();
    }

    @RequestMapping("validateName")
    @ResponseBody
    public boolean validateUsername(String name, Long id) {
        ClientDevPlan clientdevplan = clientdevplanService.getByName(name);
        //通过传回的用户名，如果查出来为null，则代表这个名字可以使用为true
        //如果查出来有值，且和传回的编辑的id相同，则相当于为同一个员工对象，允许保存，通过验证
        return clientdevplan == null || clientdevplan.getId() == id;
    }

    @RequestMapping("changeResult")
    @ResponseBody
    public JsonResult changeResult(Long id,Long traceResult) {
        try {
            clientdevplanService.changeResult(id,traceResult);
            return JsonResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.mark("操作失败");
        }
    }
    @RequestMapping("delete")
    @ResponseBody
    @RequiresPermissions(value = {"clientdevplan:delete","开发计划删除"},logical = Logical.OR)
    public JsonResult delete(Long id) {
        try {
            clientdevplanService.delete(id);
            return JsonResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.mark("操作失败");
        }
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value = {"clientdevplan:saveOrUpdate","客户开发计划新增/更改"},logical = Logical.OR)
    public JsonResult saveOrUpdate(ClientDevPlan clientdevplan) {
        try {
            if (clientdevplan.getId() != null) {
                clientdevplanService.updateByPrimaryKey(clientdevplan);
            } else {
                clientdevplanService.insert(clientdevplan);
            }
            return JsonResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.mark("保存失败");
        }
    }

    @RequestMapping("developmentFailed")
    @ResponseBody
    @RequiresPermissions(value = {"clientdevplan:developmentFailed","开发计划失败"},logical = Logical.OR)
    public JsonResult developmentFailed(Long id) {
        try {
            clientdevplanService.developmentFailed(id);
            return JsonResult.success("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.mark(e.getMessage());
        }
    }
}
